import { getYhqList,lqYhq,getMyYhqList } from '@/api/yhq/index'
export default{
	data(){
		return{
			hasYhqFlag:null,
			yhqList:[],
			lqYhqFlag:true,
			useStatus:1,
			nowPageYhq:1,
			loading:false,
			hasNextFlag:null
		}
	},
	methods:{
		//获取可领取优惠券领表
		getCanUseYhqList(type,opt){
			if(opt == 'showPop'){
				var field = { type:type,page:this.nowPageYhq,limit:3 };
				uni.showLoading({ title: '加载中...', mask: true }); 
			}else{
				var { nowPageYhq,yhqList } = this;
				this.loading = true;
				var field = { type:type,page:this.nowPageYhq,limit:this.limit };
			}
			//this.popLoading = true;
			getYhqList(field).then(res=>{
				if(opt == 'showPop'){
					if(res.data.datas.length > 0){
						this.hasYhqFlag = true;
						this.yhqList = res.data.datas;
					}else{
						this.hasYhqFlag = false;
					}
					uni.hideTabBar();
					this.$refs.yhqLqPop.open();
				}else{
					if(this.nowPageYhq == 1 && res.data.datas.length == 0){
						this.loading = false;
						this.hasYhqFlag = false;
						return;
					}
					if(res.data.datas != null){
						nowPageYhq += 1;
						yhqList.push( ...res.data.datas );
						this.yhqList = yhqList;
						this.nowPageYhq = nowPageYhq;
						this.loading = false;
						this.hasYhqFlag = true;
					}else{
						this.loading = false;
					}
				}
			}).catch(err=>{
				this.hasYhqFlag = false;
				this.loading = false;
				if(opt == 'showPop'){
					uni.hideTabBar();
					this.$refs.yhqLqPop.open();
				}
				//console.log(err)
			});
		},
		//获取我领取的优惠券
		getMyYhq(){
			let { nowPageYhq,yhqList } = this;
			this.loading = true;
			var field = { useStatus:this.useStatus,page:this.nowPageYhq,limit:this.limit };
			//console.log(field)
			getMyYhqList(field).then(res=>{
				//console.log(res)
				this.hasNextFlag = res.data.hasNext;
				if(this.nowPageYhq == 1 && res.data.datas == null){
					this.loading = false;
					this.hasYhqFlag = false;
					return;
				}
				if(res.data.datas != null){
					nowPageYhq += 1;
					yhqList.push( ...res.data.datas );
					this.yhqList = yhqList;
					this.nowPageYhq = nowPageYhq;
					this.loading = false;
					this.hasYhqFlag = true;
				}else{
					this.loading = false;
				}
			}).catch(err=>{
				this.loading = false;
				this.hasYhqFlag=false;
				//console.log(err)
			});
		},
		//领取动作
		goLqYhq(index,item,type){
			if(this.lqYhqFlag){
				this.lqYhqFlag = false;
				var field = { couponId:item.couponId };
				uni.showLoading({ title: '领取中...', mask: true });
				lqYhq(field).then(res=>{
					this.util.showToastSuc('领取成功');
					setTimeout(()=>{
						this.lqYhqFlag = true;
						this.yhqList[index].getCouponId = 10;
					},1500);
				}).catch(err=>{
					this.lqYhqFlag = true;
				});
			}
		}
	}
}